﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.IO;
using System.Windows;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Configuration;
using LocumUpdate;
using DosyaUpload;


namespace LocumUpdate
{

    public class FtpDownload : Baglanti
    {
        int timeout = 10000;

        public FtpDownload() { }

        public FtpDownload(string userName, string userPassword, string siteAdresi)
        {
            this.UserName = userName;
            this.UserPassword = userPassword;
            this.SiteAdresi = siteAdresi;
        }

        public bool getStreamDownload(string filePath, string fileName, string url)
        {
            FtpWebRequest reqFTP = null;
            FileStream outputStream = null;
            FtpWebResponse response = null;
            Stream ftpStream = null;
            bool state = true;
            try
            {
                outputStream = new FileStream(filePath + fileName, FileMode.Create);

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(this.UserName, this.UserPassword);
                reqFTP.Timeout = timeout;
                response = (FtpWebResponse)reqFTP.GetResponse();
                ftpStream = response.GetResponseStream();

                bool dlRes = Download(filePath, fileName, ftpStream, outputStream);
                if (!dlRes)
                {
                   // return false;
                    state = false;
                }
            }
            catch (Exception)
            {
                state = false;
                //return false;
            }
            finally
            {

                if (outputStream != null) outputStream.Dispose();
                if (ftpStream != null) ftpStream.Dispose();
                if (response != null) response.Close();
            }
            return state;
        }

        
        public string[] getStreamGetList(string versionConfig)
        {
            FtpWebRequest reqFTP;
            WebResponse response = null ;            
            try
            {
                StringBuilder result = new StringBuilder();

                string url = this.SiteAdresi + versionConfig;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(versionConfig)); //
                reqFTP.Credentials = new NetworkCredential(this.UserName.ToString(), this.UserPassword.ToString());
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                reqFTP.Timeout = timeout;
                response = reqFTP.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());

                string line = reader.ReadLine();
                while (line != null)
                {
                    result.Append(line);
                    result.Append("\n");
                    line = reader.ReadLine();
                }

                result.Remove(result.ToString().LastIndexOf("\n"), 1);
                reader.Close();
                response.Close();
                return result.ToString().Split('\n');
            }
            catch (Exception)
            {

                return null;

            }
            finally
            {

                if (response != null) response.Close();
            }
                        
        }

    }
}
